package com.xinxin.leetcode.problem21;

import com.xinxin.leetcode.common.ListNode;
import com.xinxin.leetcode.common.ListUtil;

/**
 * @author 史鑫鑫
 * @date 2019-08-24 1:24
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode res = new ListNode(0);
        ListNode cur = res;
        while (true) {
            if (l1 == null && l2 == null) {
                return res.next;
            }
            if (l1 == null || l2 != null && l2.val <= l1.val) {
                cur.next = new ListNode(l2.val);
                l2 = l2.next;
            } else {
                cur.next = new ListNode(l1.val);
                l1 = l1.next;
            }
            cur = cur.next;
        }
    }

    public static void main(String[] args) {
        int[] arr1 = {1, 2, 4};
        int[] arr2 = {1, 3, 4};
        ListNode l1 = ListUtil.createList(arr1);
        ListNode l2 = ListUtil.createList(arr2);
        ListUtil.printList(l1);
        ListUtil.printList(l2);
        ListNode res = new Solution().mergeTwoLists(l1, l2);
        ListUtil.printList(res);
    }
};
